﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.Reflection;
using System.IO;
using BaseDll;
namespace OtherDevice
{
    public partial class DeviceManage : UIPage
    {
        Type[] type_All = null;
        Dictionary<string,Type> dic_Type_COM = new Dictionary<string, Type>();
        Dictionary<string, Type> dic_Type_TcpIp = new Dictionary<string, Type>();
        public DeviceManage()
        {
            string strDllPath = AppDomain.CurrentDomain.BaseDirectory + "\\OtherDevice.dll";
            if (File.Exists(strDllPath))
            {
                Assembly assembly = Assembly.LoadFile(strDllPath);
                type_All = assembly.GetTypes();
                
            }
            foreach (Type t in type_All)
            {
                var tem = t.BaseType;
                if (tem != null)
                {
                    if (tem.Name == typeof(COM_Base).Name)
                    {
                        var rep = t.GetCustomAttributes(typeof(DescriptionAttribute));
                        var rep2 = rep.ToList<Attribute>();
                        string strName = rep2.Count > 0 ? ((DescriptionAttribute)rep2[0]).Description : tem.FullName;
                        dic_Type_COM.Add(strName,t);
                    }
                    if (tem.Name == typeof(TcpIp_Base).Name)
                    {
                        var rep = t.GetCustomAttributes(typeof(DescriptionAttribute));
                        var rep2 = rep.ToList<Attribute>();
                        string strName = rep2.Count > 0 ? ((DescriptionAttribute)rep2[0]).Description : tem.FullName;
                        dic_Type_TcpIp.Add(strName, t);
                    }
                }
            }
            InitializeComponent();
        }


        private void Function_AddTage(string strName,DeviceInfo deviceInfo)
        {
            dg_DeviceList.Rows.Add(strName, deviceInfo.deviceType, deviceInfo.deviceTypeName);
        }
        private void DeviceManage_Load(object sender, EventArgs e)
        {
            try
            {
                dg_DeviceList.Rows.Clear();
                foreach (var item in DeviceMgr.GetInstance().dic_DeviceInfo)
                {
                    Function_AddTage(item.Key, item.Value);
                }
                List<string> listType = new List<string>();
                cbb_SerialType.Items.Clear();
                foreach (var item in dic_Type_COM)
                {
                    cbb_SerialType.Items.Add(item.Key);
                }
                cbb_SerialType.DropDownStyle = UIDropDownStyle.DropDownList;
                if (cbb_SerialType.Items.Count >= 0)
                    cbb_SerialType.SelectedIndex = 0;


                cbb_TcpIpType.Items.Clear();
                foreach (var item in dic_Type_TcpIp)
                {
                    cbb_TcpIpType.Items.Add(item.Key);
                }
                cbb_TcpIpType.DropDownStyle = UIDropDownStyle.DropDownList;
                if (cbb_TcpIpType.Items.Count >= 0)
                    cbb_TcpIpType.SelectedIndex = 0;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void cbb_SerialType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str_TypeName = cbb_SerialType.Text;
                string strBaseTypeName = dic_Type_COM[str_TypeName].Name;
                List<string> listType = AssemblyOperate.GetAllDescriptionByName(strBaseTypeName, type_All.ToList());
                cbb_SerialPort.Items.Clear();
                foreach (var item in listType)
                {
                    cbb_SerialPort.Items.Add(item);
                }
                cbb_SerialPort.DropDownStyle = UIDropDownStyle.DropDownList;
                if (cbb_SerialPort.Items.Count >= 0)
                    cbb_SerialPort.SelectedIndex = 0;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
        private void cbb_TcpIpType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str_TypeName = cbb_TcpIpType.Text;
                string strBaseTypeName = dic_Type_TcpIp[str_TypeName].Name;
                List<string> listType = AssemblyOperate.GetAllDescriptionByName(strBaseTypeName, type_All.ToList());
                cbb_TcpIp.Items.Clear();
                foreach (var item in listType)
                {
                    cbb_TcpIp.Items.Add(item);
                }
                cbb_TcpIp.DropDownStyle = UIDropDownStyle.DropDownList;
                if(cbb_TcpIp.Items.Count>=0)
                   cbb_TcpIp.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

        }

        private void btn_AddSerial_Click(object sender, EventArgs e)
        {
            try
            {
                string str_Description = cbb_SerialPort.Text;
                Type type = AssemblyOperate.GetTypeByDescription(str_Description, type_All.ToList());
                string strTypeName = type.FullName;
                Type Test = Type.GetType(strTypeName);
                object[] args = new object[]
                {
                     strTypeName
                };
                COM_Base com = Activator.CreateInstance(Test, args) as COM_Base;
                DeviceInfo deviceInfo = new DeviceInfo();
                deviceInfo.deviceType = DeviceType.串口;
                deviceInfo.deviceTypeName = strTypeName;
                deviceInfo.Paramter_串口 = new ComParamter();
                deviceInfo.Device_串口 = com;
                DeviceMgr.GetInstance().dic_DeviceInfo.Add(txt_SerialName.Text, deviceInfo);
                Function_AddTage(txt_SerialName.Text, deviceInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
        private void btn_AddTcpIp_Click(object sender, EventArgs e)
        {
            try
            {
                string str_Description = cbb_TcpIp.Text;
                Type type = AssemblyOperate.GetTypeByDescription(str_Description, type_All.ToList());
                string strTypeName = type.FullName;
                Type Test = Type.GetType(strTypeName);
                object[] args = new object[]
                {
                  strTypeName
                };
                TcpIp_Base tcpIp = Activator.CreateInstance(Test, args) as TcpIp_Base;
                DeviceInfo deviceInfo = new DeviceInfo();
                deviceInfo.deviceType = DeviceType.TcpIp;
                deviceInfo.Paramter_TcpIp = new TcpIpParamter();
                deviceInfo.deviceTypeName = strTypeName;
                deviceInfo.Device_TcpIp = tcpIp;
                DeviceMgr.GetInstance().dic_DeviceInfo.Add(txt_TcpIpName.Text, deviceInfo);
                Function_AddTage(txt_TcpIpName.Text, deviceInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
        private void btn_AddClyliner_Click(object sender, EventArgs e)
        {
            try
            {
                ClylinerBase clyliner = new ClylinerBase();
                DeviceInfo deviceInfo = new DeviceInfo();
                deviceInfo.deviceType = DeviceType.气缸;
                deviceInfo.Paramter_气缸 = new ClylinderParamter();
                deviceInfo.Device_气缸 = clyliner;
                DeviceMgr.GetInstance().dic_DeviceInfo.Add(txt_ClylinerName.Text, deviceInfo);
                Function_AddTage(txt_ClylinerName.Text, deviceInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

        }

        private void btn_SaveParamter_Click(object sender, EventArgs e)
        {
            try
            {
                int n_Index = dg_DeviceList.SelectedIndex;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的设备"); return;
                }
                else
                {
                    string strDeviceName = dg_DeviceList.Rows[n_Index].Cells[0].Value.ToString();
                    DeviceInfo deviceInfo = new DeviceInfo();
                    deviceInfo.deviceType = (DeviceType)Enum.Parse(typeof(DeviceType), dg_DeviceList.Rows[n_Index].Cells[1].Value.ToString());
                    deviceInfo.deviceTypeName = dg_DeviceList.Rows[n_Index].Cells[2].Value?.ToString();
                    if (deviceInfo.deviceType == DeviceType.串口)
                    {
                        deviceInfo.Paramter_串口 = propertyGrid_DeviceInfo.SelectedObject as ComParamter;
                    }
                    else if (deviceInfo.deviceType == DeviceType.TcpIp)
                    {
                        deviceInfo.Paramter_TcpIp = propertyGrid_DeviceInfo.SelectedObject as TcpIpParamter;
                    }
                    else if (deviceInfo.deviceType == DeviceType.气缸)
                    {
                        deviceInfo.Paramter_气缸 = propertyGrid_DeviceInfo.SelectedObject as ClylinderParamter;
                        deviceInfo.Device_气缸 = new ClylinerBase();
                        deviceInfo.Device_气缸.Init(deviceInfo.Paramter_气缸);
                    }
                    DeviceMgr.GetInstance().dic_DeviceInfo[strDeviceName] = deviceInfo;
                    DeviceMgr.GetInstance().Save();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void btn_ReLoad_Click(object sender, EventArgs e)
        {
            try
            {
                if (UIMessageDialog.ShowAskDialog(this, "是否重新初始化所有外设？"))
                {
                    DeviceMgr.GetInstance().Function_动态创建设备();
                    DeviceMgr.GetInstance().Init();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 上移ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int n_Index = dg_DeviceList.SelectedIndex;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的设备");return;
                }
                else if(n_Index == 0)
                {
                    MessageBox.Show("第一行无法上移"); return;

                }
                else
                {
                    List<string> listValue = new List<string>();
                    for (int i = 0; i < dg_DeviceList.ColumnCount; i++)
                    {
                        listValue.Add(dg_DeviceList.Rows[n_Index].Cells[i].Value.ToString());
                    }
                    for (int i = 0; i < dg_DeviceList.ColumnCount; i++)
                    {
                        dg_DeviceList.Rows[n_Index].Cells[i].Value = dg_DeviceList.Rows[n_Index - 1].Cells[i].Value;
                        dg_DeviceList.Rows[n_Index - 1].Cells[i].Value = listValue[i];
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 下移ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int n_Index = dg_DeviceList.SelectedIndex;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的设备"); return;
                }
                else if (n_Index == dg_DeviceList.RowCount-1)
                {
                    MessageBox.Show("第一行无法上移"); return;

                }
                else
                {
                    List<string> listValue = new List<string>();
                    for (int i = 0; i < dg_DeviceList.ColumnCount; i++)
                    {
                        listValue.Add(dg_DeviceList.Rows[n_Index].Cells[i].Value.ToString());
                    }
                    for (int i = 0; i < dg_DeviceList.ColumnCount; i++)
                    {
                        dg_DeviceList.Rows[n_Index].Cells[i].Value = dg_DeviceList.Rows[n_Index + 1].Cells[i].Value;
                        dg_DeviceList.Rows[n_Index + 1].Cells[i].Value = listValue[i];
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 删除ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            try
            {
                int n_Index = dg_DeviceList.SelectedIndex;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的设备"); return;
                }
                else
                {
                    if(UIMessageDialog.ShowAskDialog(this,"是否删除所选？"))
                    {
                        string strName = dg_DeviceList.Rows[n_Index].Cells[0].Value.ToString();
                        DeviceMgr.GetInstance().dic_DeviceInfo.Remove(strName);
                        dg_DeviceList.Rows.RemoveAt(n_Index);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (UIMessageDialog.ShowAskDialog(this, "是否保存当前列表顺序？"))
                {
                     Dictionary<string, DeviceInfo> dic_DeviceInfo = new Dictionary<string, DeviceInfo>();
                    for (int i = 0; i < dg_DeviceList.RowCount; i++)
                    {
                        string strName = dg_DeviceList.Rows[i].Cells[0].Value.ToString();
                        dic_DeviceInfo.Add(strName, DeviceMgr.GetInstance().dic_DeviceInfo[strName]);
                    }
                    DeviceMgr.GetInstance().dic_DeviceInfo = dic_DeviceInfo;
                    DeviceMgr.GetInstance().Save();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void dg_DeviceList_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dg_DeviceList_SelectIndexChange(object sender, int index)
        {
            try
            {
                if (propertyGrid_DeviceInfo == null)
                    return;
                int n_Index = index;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的设备"); return;
                }
                else
                {
                    string strName = dg_DeviceList.Rows[n_Index].Cells[0].Value.ToString();
                    DeviceInfo deviceInfo = DeviceMgr.GetInstance().dic_DeviceInfo[strName];
                    if (deviceInfo.deviceType == DeviceType.串口)
                    {
                        propertyGrid_DeviceInfo.SelectedObject = deviceInfo.Paramter_串口;
                    }
                    else if (deviceInfo.deviceType == DeviceType.TcpIp)
                    {
                        propertyGrid_DeviceInfo.SelectedObject = deviceInfo.Paramter_TcpIp;
                    }
                    else if (deviceInfo.deviceType == DeviceType.气缸)
                    {
                        propertyGrid_DeviceInfo.SelectedObject = deviceInfo.Paramter_气缸;
                    }
                    propertyGrid_DeviceInfo.Tag = strName;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
